from qfluentwidgets import FluentLabelBase, getFont
from PyQt5.QtWidgets import QLabel
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QPixmap
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply


class Text15Label(FluentLabelBase):
    def getFont(self):
        return getFont(15)


class ImageLabel(QLabel):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.url = None
        self.setAlignment(Qt.AlignCenter)

    def setUrl(self, url):
        self.url = url
        self.downloadImage()

    def downloadImage(self):
        manager = QNetworkAccessManager()
        manager.finished.connect(self.onFinished)
        manager.get(QNetworkRequest(QUrl(self.url)))

    def onFinished(self, reply):
        if reply.error():
            print("图片访问失败")
            return
        else:
            data = reply.readAll()
            pixmap = QPixmap()
            pixmap.loadFromData(data)
            self.setPixmap(pixmap)
